package com.cjx.minio.util;

import java.io.File;
import java.util.Arrays;

/**
*@Description 视频工具类
*@Verson v1.0.0
*@Author cjunxian
*@Date 2020/10/29
*/
public class VideoConvertUtil {

  private VideoConvertUtil() {
    throw new IllegalStateException("Utility class");
  }

  /**
   * 判断是否是视频格式
   * @param fileName
   * @return
   */
  public static boolean canConvert(String fileName) {
    if (fileName == null || fileName.length() == 0) {
      return false;
    }

    int extIndex = fileName.lastIndexOf(".");
    if (extIndex == -1) {
      return false;
    }

    final String ext = fileName.substring(extIndex);

    // rmvb", ".rm", ".flv", ".avi", ".mkv", ".3gp", ".wmv", ".mp4"
    return Arrays.stream(new String[] {".flv", ".avi", ".mp4"})
        .anyMatch(p -> p.equalsIgnoreCase(ext));
  }

  public static String convertVideoToMp4Format(String filePath) {
    File sourceFile = new File(filePath);
    if (!sourceFile.exists()) {
      return null;
    }

    String folder = sourceFile.getParent();
    String fileName = sourceFile.getName();
    String newFilePath =
        folder
            + File.separator
            + "ffmpeg"
            + fileName.substring(0, fileName.lastIndexOf("."))
            + ".mp4";
    String cmd = "ffmpeg.exe -i " + filePath + " " + newFilePath;
    return FFmpegUtil.runCommand(cmd) != null ? newFilePath : null;
  }
}
